package com.affectu.user.action;

import java.util.Date;

import org.jboss.seam.annotations.Name;
import org.jboss.seam.ui.component.html.HtmlDiv;

import com.affectu.dao.DAOBase;
import com.affectu.message.Message;
import com.affectu.user.User;

/**
 * Sep 8, 2008
 * 
 * @author daniel nathan
 */
@Name("userDatailAction")
public class ViewDetail extends DAOBase {

	private User user;
	private HtmlDiv htmlDiv;

	public User getUser() {
		try {
			Object pId = this.getRequestParam().get("userId");
			if (pId != null) {
				user = (User) this.em.get(User.class, Long.valueOf(pId
						.toString()));
				this.getSessionScope().put("userId", pId);
			} else {
				Object sId = this.getSessionScope().get("userId");
				user = (User) this.em.get(User.class, Long.valueOf(sId
						.toString()));
			}
		} catch (Exception ex) {
			this.logException(ex);
			user = new User();
		}
		return user;
	}

	public String sendMessage(Message msg) {
		try {
			// msg.getReceivers().add(this.getUser());
			if (msg.getTopic() == null || msg.getTopic().endsWith("")) {
				msg.setTopic(this.getCurrentUser().getNickName() + "发送过来消息.");
			}
			msg.setSender(this.getCurrentUser());
			msg.setReceiver(this.getUser());
			msg.setStatus(Message.Status.NEW);
			msg.setSendDate(new Date());
			em.persist(msg);
			this.getHtmlDiv().setRendered(false);
		} catch (Exception ex) {
			this.logException(ex);
		}
		return "";
	}

	public String addAsFriend() {
		// this.getCurrentUser().
		return "";
	}

	public String preparedSendMessage() {
		this.getHtmlDiv().setRendered(true);
		return "";
	}

	public void destroy() {
		this.getSessionScope().remove("userId");
	}

	public HtmlDiv getHtmlDiv() {
		return htmlDiv;
	}

	public void setHtmlDiv(HtmlDiv htmlDiv) {
		this.htmlDiv = htmlDiv;
	}
}
